<template>
  <!--招采管理-物资招采列表-查看会签弹窗和表单-->
  <j-modal
    title="信息"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    okText="平台物资招采询价"

  >
    <a-spin :spinning="confirmLoading">
      <a-button type="primary" @click="exportExcel" icon="download" style="margin-bottom: 20px;">下载</a-button>
      <!-- <div class="no-print" style="text-align: right">
          <a-button v-print="'#printContent'" ghost type="primary">打印</a-button>
        </div> -->
      <section class="abcdefg" id="aTable" v-if="supplierDataList.length > 0">
        <table border="1" height="40px" bordercolor="#D8D8D8">
          <tr>
            <!-- <td><img src="./1.png" alt="" /></td> -->
            <td align="center" width="1051px">询价表</td>
          </tr>
        </table>
        <a-table :pagination="false"  size="middle" bordered :columns="columns" :dataSource="countersignDataList" :loading="loading"  style="margin-bottom: 20px;">
        </a-table>
        <!-- <table border="1" height="200px" bordercolor="#D8D8D8">
          <tr>
            <td align="center" width="600px">项目采购意见</td>
            <td width="600px"></td>
            <td align="center" width="600px">财务经理意见</td>
            <td width="600px"></td>
          </tr>
          <tr>
            <td align="center" width="600px">项目经理意见</td>
            <td width="600px"></td>
            <td align="center" width="600px">项目总指挥意见</td>
            <td width="600px"></td>
          </tr>
          <tr>
            <td align="center" width="600px">采购部经理意见</td>
            <td width="600px"></td>
            <td align="center" width="600px">副董事长意见</td>
            <td width="600px"></td>
          </tr>
          <tr>
            <td align="center" width="600px">成本控制中心经理意见</td>
            <td width="600px"></td>
            <td align="center" width="600px">董事长批准</td>
            <td width="600px"></td>
          </tr>
        </table> -->
      </section>
    </a-spin>
  </j-modal>
</template>

<script>
import ATextarea from 'ant-design-vue/es/input/TextArea'
import ExcelJS from 'exceljs'
import { saveAs } from 'file-saver'
import { httpAction } from '@api/manage'

//  import { mixinDevice } from '@/utils/mixin'
// import { JeecgListMixin } from '@/mixins/JeecgListMixin'
//  item.supplierName, supplierPriceItem.publicBiddingName
const renderContent = (value, row, index) => {
  const obj = {
    children: value,
    attrs: {},
  }

  // console.log(value)
  // console.log(row)
  // console.log(index)

  if (index === 1) {
    obj.attrs.rowSpan = 2
  }

  // These two are merged into above cell
  if (index === 2) {
    obj.attrs.rowSpan = 0
  }

  return obj
}

import Bus from './bus'
export default {
  name: 'huiqian',
  // mixins: [JeecgListMixin, mixinDevice],
  props: {
    value:Boolean,
    publicBiddingId: {
      type: String,
      default: '',
    },
    biddingSuppliersIds: {
      type: String,
      default: '',
    },
    // supplierDataList: {
    //   type: Array,
    //   default() {
    //     return []
    //   },
    // },
    supplierData: {
      type: Object,
      default() {
        return {}
      },
    },
    // profitabilityAnalysis: {
    //   type: String,
    //   default: '',
    // },
    // remark: {
    //   type: String,
    //   default: '',
    // },
  },
  components: {
    ATextarea,
  },
  data() {
    return {
      supplierDataList:[],
      NameProject:[],
      title: '发标',
      id: '',
      width: 1100,
      visible: false,
      disableSubmit: true,
      confirmLoading: false,

      loading: false,
      columns: [
        {
          title: '序号',
          align: 'center',
          // dataIndex: 'rowIndex',
          key:'rowIndex',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
          // customRender: renderContent,
          // customRender(_, row) {
          //   return {
          //     children: row.id,
          //     attrs: {
          //       rowSpan: row.idRowSpan,
          //     },
          //   }
          // },
        },
        {
          title: '供应商/联系人/联系电话、传真、手机',
          align: 'center',
          dataIndex: 'supplierPriceItem.supplierName',
          customRender(_, row, index) {
            // return row.supplierPriceList[index].supplierName
            return {
              children: row.supplierPriceList[0].supplierName,
              attrs: {
                rowSpan: row.supplierNameRowSpan,
              },
            }
          },
        },
        {
          title: '物资名称',
          align: 'center',
          dataIndex: 'supplierPriceItem.publicBiddingName',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].publicBiddingName,
              attrs: {
                rowSpan: row.supplierPriceList[0].publicBiddingName,
              },
            }
          },
        },
        {
          title: '型号/规格',
          align: 'center',
          dataIndex: 'supplierPriceItem.model',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].model,
              attrs: {
                rowSpan: row.supplierPriceList[0].model,
              },
            }
          },
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'supplierPriceItem.unit',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].unit,
              attrs: {
                rowSpan: row.supplierPriceList[0].unit,
              },
            }
          },
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'supplierPriceItem.num',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].num,
              attrs: {
                rowSpan: row.supplierPriceList[0].num,
              },
            }
          },
        },
        {
          title: '单价',
          align: 'center',
          dataIndex: 'supplierPriceItem.price',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].price,
              attrs: {
                rowSpan: row.supplierPriceList[0].price,
              },
            }
          },
        },
        {
          title: '金额(不含税)',
          align: 'center',
          dataIndex: 'supplierPriceItem.money',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].money,
              attrs: {
                rowSpan: row.supplierPriceList[0].money,
              },
            }
          },
        },
        {
          title: '税率',
          align: 'center',
          dataIndex: 'supplierPriceItem.taxTate',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].taxTate,
              attrs: {
                rowSpan: row.supplierPriceList[0].taxTate,
              },
            }
          },
        },
        {
          title: '金额(含税)',
          align: 'center',
          dataIndex: 'supplierPriceItem.taxMoney',
          customRender(_, row) {
            return {
              children: row.supplierPriceList[0].taxMoney,
              attrs: {
                rowSpan: row.supplierPriceList[0].taxMoney,
              },
            }
          },
        },
        {
          title: '付款方式',
          align: 'center',
          dataIndex: 'paymentMethod',
          width:'100'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',

        },
      ],
      countersignData: {},
      // url: {
      //   list: '/bidding/wtBiddingSupplier/list',
      // }
    }
  },

  created() {
  },
  computed: {
    countersignDataList() {
      let list = []

      if (this.supplierDataList.length > 0) {
        this.supplierDataList.forEach((item, itemIndex) => {
          item.supplierPriceList.forEach((supplierPriceItem, supplierPriceItemIndex) => {
            list.push({
              ...item,
              supplierPriceItem: supplierPriceItem,
            })
          })
        })

        const rowSpan = (key) => {
          let arr = list
            .reduce((result, item) => {
              if (result.indexOf(item[key]) < 0) {
                result.push(item[key])
              }
              return result
            }, [])
            .reduce((result, keys) => {
              const children = list.filter((item) => item[key] === keys)
              result = result.concat(
                children.map((item, index) => ({
                  ...item,
                  [`${key}RowSpan`]: index === 0 ? children.length : 0,
                }))
              )
              return result
            }, [])
          list = arr
        }
        rowSpan('id')
        // rowSpan('supplierName')
      }
      // console.log('3432', { list })

      return list
    },
  },
  methods: {
    handleOk(){
      let type = '平台物资招采询价';
      this.visible = false;
      let countersignDataList = JSON.stringify(this.countersignDataList)
      this.$emit('tolink',type,countersignDataList);
    },
    showInfo(record){
      this.visible = true
      this.confirmLoading = true
      httpAction('/bidding/wtPublicBidding/getCountersignData?publicBiddingId=' + record.id,{},'GET').then(res => {
        this.supplierDataList = res.result.list
        this.confirmLoading = false
        // console.log('2',res.result);
       this.NameProject=res.result
      })
    },
    add() {
      this.visible = true
    },
    edit() {
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.visible = false
    },
    exportExcel() {
      const workbook = new ExcelJS.Workbook()
      workbook.creator = 'Me'
      workbook.lastModifiedBy = 'Her'
      workbook.created = new Date(1985, 8, 30)
      workbook.modified = new Date()
      workbook.lastPrinted = new Date(2016, 9, 27)
      // 创建一个第一行和列冻结的工作表
      const sheet = workbook.addWorksheet('My Sheet', { views: [{ xSplit: 1, ySplit: 1 }] })

      // sheet.columns = [
      //   { header: '序号', key: 'id', width: 10 },
      //   { header: '供应商/联系人/联系电话、传真、手机', key: 'supplierName', width: 50, },
      //   { header: '物资名称', key: 'publicBiddingName', width: 10}
      // ];

      sheet.addRow([])
      sheet.getRow(1).height = 45

      sheet.addRow([this.NameProject.project + '' + this.NameProject.name + '询价表'])
      sheet.mergeCells(`A2:L2`)
      sheet.getCell(`A2:L2`).alignment = { vertical: 'middle', horizontal: 'center' }
      sheet.getCell(`A2:L2`).font = { size: 16, bold: true }
      // sheet.addRow(['','','','','含税中标价'])

      sheet.addRow([
      '序号',
        '供应商/联系人/联系电话、传真、手机',
        '物资名称',
        '型号/规格',
        '单位',
        '数量',
        '单价',
        '金额(不含税)',
        '税率',
        '金额(含税)',
        '付款方式',
        '备注',
      ])
      // sheet.mergeCells(`E3:F3`)
      // sheet.mergeCells(`A3:A4`)
      // sheet.getCell('A3').value='序号'
      // sheet.mergeCells(`B3:B4`)
      // sheet.getCell('B3').value='项目名称'
      // sheet.mergeCells(`C3:C4`)
      // sheet.getCell('C3').value='计量单位'
      // sheet.mergeCells(`D3:D4`)
      // sheet.getCell('D3').value='工程量'
      let rowNum = 3

      this.countersignDataList.forEach((item, itemIndex) => {
        rowNum++
        // console.log({item});
        sheet.addRow([
          itemIndex,
            // item.name,
            item.supplierPriceItem.supplierName,
            item.supplierPriceItem.publicBiddingName,
            item.supplierPriceItem.model,

            item.supplierPriceItem.unit,
            item.supplierPriceItem.num,
            item.supplierPriceItem.price,
            item.supplierPriceItem.money,
            item.supplierPriceItem.taxTate,
            item.supplierPriceItem.taxMoney,
            // item.supplierPriceItem.taxTate,
            // supplierPriceItem.taxTate,
            // supplierPriceItem.taxMoney,
            item.paymentMethod,
            item.remark,
          ])
          // console.log('21',rowNum);
          sheet.getRow(`${rowNum}`).alignment = {
            vertical: 'middle',
            horizontal: 'center',
            wrapText: true,
          }
      })
      const alignment = { vertical: 'middle', horizontal: 'center', wrapText: true }

      rowNum++
      sheet.getCell(`A${rowNum }`).value = '项目采购意见'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '财务经理意见'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '项目经理意见'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '	项目总指挥意见'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '采购部经理意见'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '副董事长意见'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '成本控制中心经理意见'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '董事长批准'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment

      // this.supplierDataList.forEach((item, itemIndex) => {
      //   let startRowNum = rowNum
      //   // debugger
      //   item.supplierPriceList.forEach((supplierPriceItem, supplierPriceItemIndex) => {
      //     rowNum++
      //     // sheet.addRow({id: item.id, supplierName: item.supplierName, publicBiddingName: supplierPriceItem.publicBiddingName});
      //     // sheet.addRow({id: item.id, supplierName: item.supplierName, publicBiddingName: supplierPriceItem.publicBiddingName});

      //     sheet.addRow([
      //     itemIndex,
      //       // item.name,
      //       supplierPriceItem.name,
      //       supplierPriceItem.materialsUnit,
      //       supplierPriceItem.num,

      //       supplierPriceItem.price,
      //       supplierPriceItem.money,
      //       // supplierPriceItem.taxTate,
      //       // supplierPriceItem.taxMoney,
      //       // item.paymentMethod,
      //       // item.remark,
      //     ])
      //   console.log(`A${startRowNum}:A${rowNum - 1}`);

      //     sheet.getCell(`A${rowNum -1}`).alignment = {
      //       vertical: 'middle',
      //       horizontal: 'center',
      //       wrapText: true,
      //     }
      //   })

        // sheet.mergeCells(`A${startRowNum}:A${rowNum - 1}`)
        // sheet.mergeCells(`B${startRowNum}:B${rowNum - 1}`)
        // sheet.mergeCells(`C${startRowNum}:C${rowNum - 1}`)
        // sheet.mergeCells(`D${startRowNum}:D${rowNum - 1}`)
        // sheet.mergeCells(`E${startRowNum}:E${rowNum - 1}`)
        // sheet.mergeCells(`F${startRowNum}:F${rowNum - 1}`)
        // sheet.mergeCells(`J${startRowNum}:J${rowNum - 1}`)
        // sheet.mergeCells(`H${startRowNum}:H${rowNum - 1}`)
        // sheet.mergeCells(`I${startRowNum}:I${rowNum - 1}`)
        // sheet.mergeCells(`G${startRowNum}:G${rowNum - 1}`)
        // sheet.mergeCells(`K${startRowNum}:K${rowNum - 1}`)
        // sheet.mergeCells(`L${startRowNum}:L${rowNum - 1}`)
        // console.log(`A${startRowNum}:A${rowNum - 1}`);
        // console.log(`A${startRowNum}:A${rowNum - 1}`);
        // sheet.getCell(`A${startRowNum}:A${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`C${startRowNum}:C${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`D${startRowNum}:D${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`E${startRowNum}:E${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`F${startRowNum}:F${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`J${startRowNum}:J${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`H${startRowNum}:H${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`I${startRowNum}:I${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`G${startRowNum}:G${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`K${startRowNum}:K${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`L${startRowNum}:L${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getCell(`B${startRowNum}:B${rowNum - 1}`).alignment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // const ment = {
        //   vertical: 'middle',
        //   horizontal: 'center',
        //   wrapText: true,
        // }
        // sheet.getRow(3).alignment = ment

        // sheet.getCell(`A${startRowNum}:A${rowNum - 1}`).font = {
        //   // color: { argb: 'FF00FF00' },
        // }

        // sheet.getCell(`A${startRowNum}:A${rowNum - 1}`)
      // })
        // sheet.getCell('B' + rowNum).value = 'XXXX1'
      // sheet.getCell('B' + rowNum + 1).value = 'XXXX1'
       // sheet.getCell('A' + rowNum + 1).value = '项目经理意见'

      // const row = sheet.getRow(5,7,8,9,10)
      // row.height = 42.5

      const imageId1 = workbook.addImage({
        base64:
          '',
        extension: 'jpeg',
      })


      sheet.addImage(imageId1, {
        tl: { col: 0, row: 0 },
        ext: { width: 173, height: 55 },
        editAs: 'absolute',
      })

      workbook.xlsx.writeBuffer().then((data) => {
        const blob = new Blob([data], { type: '' })
        saveAs(blob, this.NameProject.project + '' + this.NameProject.name + '询价表' + '.xlsx')
      })

      // $("#aTable").table2excel({
      //    exclude: ".noExl",
      //    sheetName: "询价表",
      //    filename: "询价表",
      //    exclude_img: true,
      //    exclude_links: true,
      //    exclude_inputs: true
      // });
    },


  },
}
</script>
<style scoped>
.div1 {
  width: 100%;
  text-align: center;
  font-size: 16px;
}
</style>